//
//  MyPostCollectionView.m
//  FlowerRainGround
//
//  Created by zfc on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MyPostCollectionView.h"

#import "HeaderCollectionReusableView.h"
#import "FooterCollectionReusableView.h"
#import "PicFooterView.h"
#import "PicHeaderView.h"
#import "PicCollectionVeiwCell.h"

@interface MyPostCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic)CGFloat imagewidth;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) RecommendModel *model;
@end

@implementation MyPostCollectionView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
        
    }
    return self;
}

- (NSMutableArray *)picArray {
    if (!_picArray) {
        self.picArray = [NSMutableArray array];
    }
    return _picArray;
}


- (UICollectionView *)createPostCollectionViewWithModel:(RecommendModel *)model {
    self.model = model;
    [self getPicArrayWithModel:self.model];
    
    [self createCollectionView];
    return self.collectionView;
}


- (NSMutableArray *)getPicArrayWithModel:(RecommendModel *)model {
    
    self.model = model;
    self.picArray = [[model.attachment componentsSeparatedByString:@","] mutableCopy];
    
    return self.picArray;
}

//获取列数
- (NSInteger)getRowNumber {
    
    if (self.picArray.count % 3 == 0) {
        return self.picArray.count / 3;
    } else {
        return self.picArray.count / 3 + 1;
    }
}


- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _imagewidth = (kWidth - 24) / 3;
    layout.itemSize = CGSizeMake(_imagewidth, _imagewidth);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.minimumLineSpacing = 2;
    layout.minimumInteritemSpacing = 2;
    //    layout.headerReferenceSize = CGSizeMake(kWidth, kHeight * 2 / 3);
    
    
    //    NSInteger number = [self getRowNumber];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self addSubview:self.collectionView];
    _collectionView.bounces = YES;
    [_collectionView registerNib:[UINib nibWithNibName:@"PicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"zz"];
    [self.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
}

#pragma mark --- 集合视图代理方法 ---

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}

- (UICollectionViewCell *)
collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PicCollectionVeiwCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zz" forIndexPath:indexPath];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.picArray[indexPath.row]]];
    
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        PicHeaderView *h = [PicHeaderView headerView];
        h.frame = headerView.frame;
        [headerView addSubview:h];
        [h.headImg sd_setImageWithURL:[NSURL URLWithString:self.model.customerModel.headImg]];
        h.userNameLabel.text = self.model.customerModel.userName;
        h.contentLabel.text = self.model.customerModel.content;
        h.createDataLabel.text = self.model.customerModel.createDate;
        
        return headerView;

    } else {
        
        FooterCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        PicFooterView *p = [PicFooterView footerView];
        p.frame = CGRectMake(0, 0, kWidth, 80);
        [footerView addSubview:p];
        
        p.contentLabel.text = self.model.content;
        p.commentLabel.text = [NSString stringWithFormat:@"%@",self.model.comment];
        p.shareLabel.text = [NSString stringWithFormat:@"%@",self.model.share];
        p.appointLabel.text = [NSString stringWithFormat:@"%@",self.model.appoint];
        
        return footerView;
    }
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(kWidth, kHeight - (84 + _imagewidth * 3));
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kWidth, 80);
    
}





@end
